﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using OA.BLL;

using OA.Core;
using OA.Core.EF;
using OA.Core.Helper;
using OA.Model;
using OA.Web.Attribute;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using X.PagedList;

namespace OA.Web.Areas.Member.Controllers
{
    public class DiaryController : BaseMemberController
    {
        private IMemoryCache _cache;
        private ICookie _cookie;
        public DiaryController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
            _cookie = cookie;
        }

        [MyActionFilterAttribute("001009", "工作日志", "个人中心", true, "iconwodegongzuorizhi")]
        public IActionResult Index()
        { 
            return View( );
        }

        [MyActionFilterAttribute("001009", "工作日志")]
        public IActionResult GetData(string startTime, string endTime, int page = 1,int limit=20)
        {
            Expression<Func<Diary, bool>> where = m => m.EmpID == Info.EmpID;
            if (!string.IsNullOrWhiteSpace(startTime))
            {
                DateTime dt = DateTime.Now;
                if (DateTime.TryParse(startTime, out dt))
                {
                    string tempStr = dt.ToString("yyyy-MM-dd");
                    where = where.AndAlso(m => m.DateTime.CompareTo(tempStr) >= 0);
                }
            }

            if (!string.IsNullOrWhiteSpace(endTime))
            {
                DateTime dt = DateTime.Now;
                if (DateTime.TryParse(endTime, out dt))
                {
                    string tempStr = dt.ToString("yyyy-MM-dd");
                    where = where.AndAlso(m => m.DateTime.CompareTo(tempStr) <= 0);
                }
            }
            var list = this.dbContextFactory.DbContext.Diary.Where(where).OrderByDescending(m => m.ID).ToPagedList(page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.TotalItemCount,
                data = list
            };
            return Json(result);
        }

        [HttpGet]
        [MyActionFilterAttribute("001009", "工作日志")]
        public IActionResult Create()
        {             
            return View(new Diary());
        }

        [HttpPost]
        [MyActionFilterAttribute("001009", "工作日志")]
        public IActionResult Create(Diary info)
        {
            BaseResult baseResult = new BaseResult();
            DateTime dt;
            if(string.IsNullOrWhiteSpace(info.DateTime))
            {
                baseResult.State = 400;
                baseResult.Msg = "日期不能为空";
                return Json(baseResult);
            }

            if(!DateTime.TryParse(info.DateTime,out dt))
            {
                baseResult.State = 400;
                baseResult.Msg = "日期格式不正确";
                return Json(baseResult);
            }
            info.DateTime = dt.ToString("yyyy-MM-dd");
            info.EmpID = Info.EmpID;
            info.EmpName = Info.EmpName;
            Diary infoDiary=this.dbContextFactory.DbContext.Diary.Where(m => m.EmpID == info.EmpID && m.DateTime == info.DateTime).FirstOrDefault();
            if (infoDiary != null)
            {
                this.Update<Diary>(infoDiary,info,new string[] { "Details" });
            }
            else
            {
                info.Status = 1;
                this.AddInfo<Diary>(info);
            }
            baseResult.Msg = "工作日志添加成功！";
            return Json(baseResult);
        }

        [HttpGet]
        [MyActionFilterAttribute("001009", "工作日志")]
        public IActionResult Edit(int id)
        {
            Diary infoDiary = this.GetInfo<Diary>(id);
            if (infoDiary == null)
            {
                return FinshOther("没有找到该日志记录");
            }
            return View(infoDiary);
        }

        [HttpPost]
        [MyActionFilterAttribute("001009", "工作日志")]
        public IActionResult Edit(Diary info)
        {
            BaseResult baseResult = new BaseResult();
            DateTime dt;
            if (string.IsNullOrWhiteSpace(info.DateTime))
            {
                baseResult.State = 400;
                baseResult.Msg = "日期不能为空";
                return Json(baseResult);
            }

            if (!DateTime.TryParse(info.DateTime, out dt))
            {
                baseResult.State = 400;
                baseResult.Msg = "日期格式不正确";
                return Json(baseResult);
            }
            info.DateTime = dt.ToString("yyyy-MM-dd");
            Diary infoDiary = this.GetInfo<Diary>(info.ID);
            if (infoDiary != null)
            {
                this.Update<Diary>(infoDiary, info, new string[] { "Details" });
                baseResult.Msg = "工作日志修改成功！";
                return Json(baseResult);
            }
            else
            {
                info.EmpID = Info.EmpID;
                info.Status = 1;
                this.AddInfo<Diary>(info);
                baseResult.Msg = "工作日志添加成功！";
                return Json(baseResult);
            }
            
        }

        [HttpGet]
        [MyActionFilterAttribute("001009", "工作日志")]
        public IActionResult Delete(int id)
        {
            Expression<Func<Diary, bool>> where = m => m.ID == id && m.EmpID == Info.EmpID;
            this.Delete<Diary>(where);
            return Json(new { State = 200, Message = "" });
        }

    }
}